package com.nd.qa.shareplate.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nd.qa.shareplate.IDao.ElementMapper;
import com.nd.qa.shareplate.common.CPResultCode;
import com.nd.qa.shareplate.model.ElementAction;
import com.nd.qa.shareplate.model.PageElement;
import com.nd.qa.shareplate.service.PageElementService;

@Service("com.nd.qa.shareplate.service.PageElementService")
public class PageElementServiceImpl implements PageElementService{

	@Autowired
	private ElementMapper elementDao;
	
	@Override
	public HashMap<String, Object> insertElememt(PageElement element) throws Exception{
		elementDao.insertElememt(element);
		HashMap<String, Object> returnMap = new HashMap<String,Object>();
		returnMap.put("code", CPResultCode.CP0000);
		returnMap.put("message", "新增元素信息成功");
		return returnMap;
	}

	@Override
	public void updateElement(PageElement element) throws Exception{
		elementDao.updateElement(element);
	}

	@Override
	public void deleteElement(PageElement element) throws Exception{
		elementDao.deleteElement(element);
	}

	@Override
	public List<HashMap<String, Object>> queryElementType() throws Exception{
		
		return elementDao.queryElementType();
	}

	@Override
	public List<HashMap<String, Object>> queryElementAction() throws Exception{
		
		return elementDao.queryElementAction();
	}

	@Override
	public List<HashMap<String, Object>> queryElementById(PageElement pageElement) throws Exception{
		return elementDao.queryElementById(pageElement);
	}

	@Override
	public List<HashMap<String, Object>> queryLimitElement(HashMap<String, Object> paramMap) throws Exception{
		return elementDao.queryLimitElement(paramMap);
	}

	@Override
	public Integer quertElmSeqByName(String seqName) throws Exception {
		return elementDao.quertElmSeqByName(seqName);
	}
	@Override
	public PageElement querySingleElementById(Integer elementId) throws Exception {
		return elementDao.querySingleElementById(elementId);
	}

	@Override
	public List<PageElement> queryElementsByIdList(List<Integer> elementIdList)
			throws Exception {
		return elementDao.queryElementsByIdList(elementIdList);
	}

	@Override
	public PageElement queryChildElementById(Integer elementId) throws Exception{
		return elementDao.queryChildElementById(elementId);
	}

	@Override
	public List<PageElement> queryElementForPageId(int pageId) throws Exception {
		List<PageElement> elements = elementDao.queryElementForPageId(pageId);
		for (int i = 0; i < elements.size(); i++) {
			if(elements.get(i).getElementContent() == null){
				elements.get(i).setElementContent("");
			}
			if(elements.get(i).getElementTypeName() == null){
				elements.get(i).setElementTypeName("TextView");
			}
		}	
		return elements;
	}

	@Override
	public PageElement queryTargetElement(PageElement pageElement) throws Exception {
		return elementDao.queryTargetElement(pageElement);
	}

	@Override
	public PageElement queryElementInfoByElemId(PageElement pageElement) throws Exception {
		return elementDao.queryElementInfoByElemId(pageElement);
	}

	@Override
	public void updateCopyElements(PageElement element) {
		elementDao.updateCopyElements(element);
	}

	@Override
	public Integer queryMaxElementId(Integer pageId) throws Exception {
		return elementDao.queryMaxElementId(pageId);
	}

	@Override
	public Integer updateElmentInfo(HashMap<String, Object> eleParam) {
		Integer res = elementDao.updateElmentInfo(eleParam);
		return res;
	}

	@Override
	public void updateElementById(HashMap<String, Object> pElment) throws Exception {
		elementDao.updateElementById(pElment);
	}
}
